import java.sql.Date;
import java.sql.Time;
import java.util.Scanner;

public class TreatmentRecordMessageEditor {

	/**
	 * This variable is the juice of the break.
	 */
	public static final String breakSign = MessageEditor.breakSign;
	
	/**
	 * This variable needs to have at least one space on each side of it ex: " breakSign "
	 */
	public static final String dBreak = " " + breakSign + " ";
	
	public static TreatmentRecord getTreatmentRecord(String message){
		try{
			Scanner scan = new Scanner(message);

			if(!scan.next().equals("TREATMENTRECORD")) return null;
			if(!scan.next().equals(breakSign)) return null;

			TreatmentRecord tr = new TreatmentRecord();
			Date date = Date.valueOf(scan.next());
			tr.setDate(date);
			if(!scan.next().equals(breakSign)) return null;
			
			Time time = Time.valueOf(scan.next());
			if(!scan.next().equals(breakSign)) return null;
			tr.setTime(time);

			String tDoctor, aNurse, symptoms, diagnosis;
			tDoctor = "";
			aNurse = "";
			symptoms = "";
			diagnosis = "";
			String next = scan.next();

			while(!next.equals(breakSign)){
				tDoctor += next + " ";
				next = scan.next();
			}next = scan.next();

			while(!next.equals(breakSign)){
				aNurse += next + " ";
				next = scan.next();
			}next = scan.next();

			while(!next.equals(breakSign)){
				symptoms += next + " ";
				next = scan.next();
			}next = scan.next();
			
			while(!next.equals(breakSign)){
				diagnosis += next + " ";
				next = scan.next();
			}
			
			tr.setTreatingDoctor(tDoctor);
			tr.setAttendingNurse(aNurse);
			tr.setSymptoms(symptoms);
			tr.setDiagnosis(diagnosis);

			tr.setBpDiastolic(Integer.parseInt(scan.next()));
			if(!scan.next().equals(breakSign)) return null;

			tr.setBpSystolic(Integer.parseInt(scan.next()));
			if(!scan.next().equals(breakSign)) return null;

			tr.setPulse(Integer.parseInt(scan.next()));
			if(!scan.next().equals(breakSign)) return null;
			
			tr.setTemperature(Double.parseDouble(scan.next()));
			if(!scan.next().equals(breakSign)) return null;

			tr.setWeight(Double.parseDouble(scan.next()));
			if(!scan.next().equals(breakSign)) return null;

			String msg = scan.next();
			tr.setHeight(HeightMessageEditor.getHeight(msg));
			if(!scan.next().equals(breakSign)) return null;

			String prescription, labwork, followUpInstructions, otherInstructions;
			prescription = "";
			labwork = "";
			followUpInstructions = "";
			otherInstructions = "";
			next = scan.next();
			
			while(!next.equals(breakSign)){
				prescription += next + " ";
				next = scan.next();
			}next = scan.next();
			
			while(!next.equals(breakSign)){
				labwork += next + " ";
				next = scan.next();
			}next = scan.next();
			
			while(!next.equals(breakSign)){
				followUpInstructions += next + " ";
				next = scan.next();
			}next = scan.next();
			
			while(!next.equals(breakSign)){
				otherInstructions += next + " ";
				next = scan.next();
			}
			
			tr.setPrescription(prescription);
			tr.setLabWork(labwork);
			tr.setFollowUpInstructions(followUpInstructions);
			tr.setOtherInstructions(otherInstructions);

			if(!scan.next().equals("END")) return null;
			
			return tr;

		}catch(Exception e){
			return null;
		}
		
	}
	
	public static String getMessage(TreatmentRecord rec){
		if(rec == null){
			return "NULL" + dBreak + "END";
		}else{
			String tr = "TREATMENTRECORD" + dBreak;
			
			tr += rec.getDate().toString() + dBreak +
				  rec.getTime().toString() + dBreak +
				  rec.getTreatingDoctor() + dBreak +
				  rec.getAttendingNurse() + dBreak +
				  rec.getSymptoms() + dBreak +
				  rec.getDiagnosis() + dBreak +
				  rec.getBpDiastolic() + dBreak +
				  rec.getBpSystolic() + dBreak +
				  rec.getPulse() + dBreak +
				  rec.getTemperature() + dBreak +
				  rec.getWeight() + dBreak;
			
			String height = HeightMessageEditor.getMessage(rec.getHeight());
			
			tr += height + dBreak;
			
			tr += rec.getPrescription() + dBreak +
				  rec.getLabWork() + dBreak +
				  rec.getFollowUpInstructions() + dBreak +
				  rec.getOtherInstructions() + dBreak +
				  "END";
			
			return tr;
		}
	}
	
	
	
	/* THIS IS THE MAIN METHOD TO TEST THE getMessage(),
	 * getTreatmentRecord(), getHeightMessage(), and
	 * getHeight() METHODS.
	 * 
	public static void main(String[] args){
		
		// THIS CHECKS THE getTreatmentRecord() AND getMessage() METHODS
		TreatmentRecord tr = new TreatmentRecord();
		tr.setAttendingNurse("Sally Jones");
		tr.setBpDiastolic(4);
		tr.setBpSystolic(16);
		tr.setDate(new Date(12, 10, 12));
		tr.setDiagnosis("The patient is very, very sick.");
		tr.setFollowUpInstructions("Do stuff after this appointment.");
		tr.setOtherInstructions("Do other stuff too.");
		tr.setHeight(new Height(12, 12));
		tr.setLabWork("This lab work needs to get done.");
		tr.setPrescription("Take these pills.");
		tr.setPulse(17);
		tr.setSymptoms("This stuff is happening");
		tr.setTemperature(5.68);
		tr.setTime(new Time(3, 45, 32));
		tr.setTreatingDoctor("Dr. Jim Somebody");
		tr.setWeight(8.9);
		
		System.out.println("\n\n\nHERE IS THE FIRST TREATMENT RECORD\n");
		System.out.println(tr + "\n\n");
		
		String msg = getMessage(tr);
		System.out.println(msg);
		
		TreatmentRecord rt = getTreatmentRecord(msg);
		System.out.println(rt);
		
	}*/
	
}
